<template>
  <work-template v-model="detailForm" @submitOn="submitOn" :flowId="flowId"
                 @closeSelectedTag="closeSelectedTag" :butShow="bool">
    <div slot="form" class="container-div">
      <!--基础信息-->
      <basic-info v-model="detailForm" v-if="detailForm.insuranceChannels !== 2"
                  :state="isEdit" :isDownload="isDownload"/>

      <hoji-offer-info v-if="detailForm.insuranceChannels === 2"
                       v-model="detailForm" :isDownload="isDownload"/>
      <!--交强险信息(店保 客户)-->
      <compulsory-Traffic-Tnsurance v-if="detailForm.insuranceChannels !== 1" v-model="detailForm" ref="refCompulsory"
                                    :state="isEdit"/>
      <!--商业险信息 (店保 客户)-->
      <commercia-insurance v-if="detailForm.insuranceChannels !== 1" v-model="detailForm" ref="refCommercia"
                           :state="isEdit"/>
      <!--投保要求(我司)-->
      <division-purchase v-if="detailForm.insuranceChannels === 1" v-model="detailForm" ref="refDivision"
                         :state="isEdit"/>
      <!--其他保险-->
      <otherPrice v-model="detailForm"
                  v-if="detailForm.insuranceChannels === 1"
                  :switchBoole="true" :state="isEdit" ref="refOtherPrice"/>

      <other v-model="detailForm" :switchBoole="true"
             v-if="[2,3].includes(Number(detailForm.insuranceChannels))"
             :state="isEdit" ref="refOther"/>


      <!--批量（店保 ）-->
      <vehicle-entry v-model="detailForm" ref="refVehicleEntry"
                     v-if="detailForm.insuranceChannels === 2 && bool" :state="isEdit"/>
      <!--备注-->
      <remark v-if="detailForm.insuranceChannels !== 1" ref="refRemark" v-model="detailForm" :state="isEdit"/>
    </div>

  </work-template>
</template>

<script>
import {getToken} from "@/utils/auth";

import {
  addInsuranceList,
  newAddInsuranceList,
  getWorkDetails,
} from "@/api/cwgd/insureApi.js";
import {batchData} from "@/api/cwgd/cwgdApi";
import basicInfo from "../components/carWorkOrder/basicInfo.vue";
import compulsoryTrafficTnsurance from "../components/carWorkOrder/compulsoryTrafficTnsurance.vue";
import commerciaInsurance from "../components/carWorkOrder/commerciaInsurance.vue";
import divisionPurchase from "../components/carWorkOrder/divisionPurchase.vue";
import other from "../components/carWorkOrder/other.vue";
import otherPrice from "../components/carWorkOrder/otherPrice.vue";
import clientBatch from "../components/carWorkOrder/clientBatch.vue";
import vehicleEntry from "../components/carWorkOrder/vehicleEntry.vue";
import remark from "../components/carWorkOrder/remark.vue";

export default {
  dicts: ["car_business_type", "insurance_channels"],
  name: "NewCarInsuranceDetails",
  components: {
    basicInfo,
    hojiOfferInfo: () => import("../components/carWorkOrder/hojiOfferInfo.vue"),
    compulsoryTrafficTnsurance,
    commerciaInsurance,
    divisionPurchase,
    other,
    clientBatch,
    vehicleEntry,
    remark,
    otherPrice
  },


  computed: {
    isShow() {
      if (this.$route.query.type === "view") {
        return true;
      } else {
        return false;
      }
    },
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    flowId() {
      if (this.$route.query.flowId) {
        return this.$route.query.flowId
      } else {
        return null
      }
    },
    isDownload() {
      if (this.$route.query.type === "edit"
          && (this.detailForm.insuranceChannels === 2 || this.detailForm.insuranceChannels === 1)) {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    }
  },

  watch: {},

  data() {
    return {
      businessVoList: [], // 我司购买保险套餐
      insuranceList: [
        {value: "1", label: "运营"},
        {value: "2", label: "非运营"},
      ],
      inspectDetail: {},
      isForm: false,
      method: {},

      // 表单参数
      detailForm: {
        //基础信息
        carCount: "",
        brandName: "",
        carId: "",
        carNo: "",
        contractReportNumber: "",
        decorateProList: "",
        guidancePrice: "",
        insuranceChannels: "",
        modelName: "",
        operationsName: "",
        procurementReportNumber: "",
        propertyRightName: "",
        purchasePrice: "",
        rentInsuranceCompany: "",
        seriesName: "",
        surplusTotalPrice: "",
        totalPrice: "",
        vinNo: "",
        workId: "",

        //交强险信息
        jqxCompany: "",
        jqxPolicyCode: "",
        jqxCompanyCode: "",
        jqxCompanyId: "",
        jqxExpectBeginTime: "",
        jqxExpectEndTime: "",
        jqxInsuranceType: "",
        jqxAmount: "",
        carTaxStatus: 1,
        carTax: "",
        jqxPolicyUrl: "",
        jqxInsuredName: "",

        //商业险信息
        syxCompany: "",
        syxCompanyId: "",
        syxPolicyCode: "",
        syxCompanyCode: "",
        syxExpectBeginTime: "",
        syxExpectEndTime: "",
        syxInsuranceType: "1",
        syxAmount: "",
        businessList: [],
        insuranceVoList: [],
        syxPolicyUrl: "",
        syxInsuredName: "",
        otherOpenStatus: 0,
        workIdList: [],
        attachmentUrl:'',
        dbFirstPolicyUrl:'',
        addProStatus:1
      },
      detailListForm: [],
      // 交强险1
      compulsoryForm: {},
      // 商业险2
      commercialForm: {},
    };
  },

  created() {
    // this.insuranceDetails();
    this.method = this.$route.query;

    this.handleData();

  },
  mounted() {
  },
  methods: {
    // 获取详情
    async handleData() {
      let id = this.method?.businessDataId || null;
      let data = {}
      try {
        if (this.$route.query.workOrderStatus == 1) {
          //处理详情
          const res = await batchData([this.$route.query.id])
          data = this._.cloneDeep(res.data)
        }

        if (id && ((this.$route.query.workOrderStatus == 5) || this.$route.query.type === 'view')) {
          //详情总
          const resS = await getWorkDetails(id)
          data = this._.cloneDeep(resS.data)
        }

        this.detailForm = data;
        this.detailForm.otherOpenStatus = data?.otherOpenStatus || 0
        if (!this.detailForm.jqxInsuredName) {
          this.detailForm.jqxInsuredName = this.$route.query.propertyRightName || ''
        }
        if (!this.detailForm.syxInsuredName) {
          this.detailForm.syxInsuredName = this.$route.query.propertyRightName || ''
        }
      } catch (e) {
        console.error(e)
      }
    },

    // 提交操作
    submitOn() {
      let refs;


      if (this.detailForm.insuranceChannels === 1) {
        let refDivision = this.$refs.refDivision.handleEffect();
        let refOtherPrice = this.$refs.refOtherPrice.handleEffect();
        refs = [refDivision, refOtherPrice];
      }
      if (this.detailForm.insuranceChannels === 2) {
        let refCompulsory = this.$refs.refCompulsory.handleEffect();
        let refCommercia = this.$refs.refCommercia.handleEffect();
        let refOther = this.$refs.refOther.handleEffect();
        let refVehicleEntry = this.$refs.refVehicleEntry.handleEffect();
        let refRemark = this.$refs.refRemark.handleEffect();
        refs = [refCompulsory, refCommercia, refOther, refVehicleEntry, refRemark];
      }
      if (this.detailForm.insuranceChannels === 3) {
        let refCompulsory = this.$refs.refCompulsory.handleEffect();
        let refCommercia = this.$refs.refCommercia.handleEffect();
        let refOther = this.$refs.refOther.handleEffect();
        refs = [refCompulsory, refCommercia, refOther]
      }

      Promise.all(refs)
          .then(() => {
            const loading = this.$loading({
              lock: true,
              text: "保存中",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)",
            });

            this.detailForm.workId = this.method.id;
            newAddInsuranceList(this.detailForm)
                .then((res) => {
                  if (res.code === 200) {
                    loading.close();
                    this.closeSelectedTag(this.$route);
                  }
                })
                .catch(() => {
                  loading.close();
                });
          })
          .catch(() => {
          });
    },

    //保存


    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {
        this.$router.push({
          path: "/cwgd/newCarInsuranceList",
        });
      });
    },

  },
};
</script>


<style lang="scss" scoped>
@import "@/styles/cwgd/container.scss";
</style>
